package com.yun.datastructure.linear.stack.leetcode;

import java.util.Stack;

public class Leetcode20 {
    public boolean isValid(String s){
        //ArrayStack<Character> stack=new ArrayStack<>(s.length());
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c=s.charAt(i);
            if(c=='('){
                stack.push(')');
            }else if(c=='['){
                stack.push(']');
            }else if(c=='{'){
                stack.push('}');
            }else {
                if(stack.empty() || c!=stack.pop()){
                    return false;
                }
            }
        }
        return stack.empty();
    }
    /*
        ( [ ]

        底-栈-顶
        ) ]
     */
    public static void main(String[] args) {
        Leetcode20 s=new Leetcode20();
        System.out.println(s.isValid("([{}])"));
        System.out.println(s.isValid("()[]{}"));
        System.out.println(s.isValid("()"));
        System.out.println("---------------------");

        System.out.println(s.isValid("[)"));
        // ]
        System.out.println(s.isValid("([)]"));
        // ) ]
        System.out.println(s.isValid("([]"));
        // )
        System.out.println(s.isValid("("));

        System.out.println("---------------------");
        System.out.println(s.isValid(")("));
        System.out.println(s.isValid("]"));
    }
}
